var xmlhttp;
var xml;

function getXmlHttpObject()
{
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest();
    }
    if (window.ActiveXObject)
    {
        // code for IE6, IE5
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    return null;
}

function categoryStateChanged()
{
    if (xmlhttp.readyState == 4)
    {
        // load the xml response into an xml object
        xml = xmlhttp.responseXML;

        // clear the element of any previous options
        var componentList = document.getElementById("component");
        componentList.innerHTML = "";
        
        // create a default option with value of 0
        var listOption = document.createElement("option");
        listOption.textContent = "Select";
        listOption.value = "0";
        componentList.options.add(listOption);

        // loop through the xml response until all components are in the list
        var components = xml.getElementsByTagName('components');
        for (var i = 0; i < components[0].childNodes.length; i++) 
        {
            // get each component node from the component list
            var component = components[0].childNodes[i];

            // create the list option and add it to the list
            var option = document.createElement("option");
            option.text = "ID: " + component.getElementsByTagName("id")[0].textContent + ", Name: " + component.getElementsByTagName("name")[0].textContent;
            option.value = component.getElementsByTagName("id")[0].textContent;
            componentList.options.add(option);
        }
    }
}

function componentStateChanged()
{
    if (xmlhttp.readyState == 4)
    {
        xml = xmlhttp.responseXML;
        var form = document.getElementById("editForm");
        form.name.value = xml.getElementsByTagName("name")[0].childNodes[0].nodeValue;
        form.description.value = xml.getElementsByTagName("description")[0].childNodes[0].nodeValue;
        form.price.value = xml.getElementsByTagName("price")[0].childNodes[0].nodeValue;
        form.stock.value = xml.getElementsByTagName("stock")[0].childNodes[0].nodeValue;
    }
}

function clearFormData(form)
{
    try
    {
        form.name.value = "";
        form.description.value = "";
        form.price.value = "";
        form.stock.value = "";
    }
    catch (error)
    {
        //do nothing
    }
}

function getComponents(form)
{
    var catId = form.category.options[form.category.options.selectedIndex].value;
    var url = "ajax/AjaxComponents.php?action=getCompList&cat=" + catId;

    clearFormData(form);

    xmlhttp = getXmlHttpObject();
    xmlhttp.onreadystatechange = categoryStateChanged;
    xmlhttp.open("POST", url, true);
    xmlhttp.send(null);
}

function getComponent(form)
{
    var compId = form.component.options[form.component.options.selectedIndex].value;
    var url = "ajax/AjaxComponents.php?action=getComp&comp=" + compId;

    clearFormData(form);

    if (compId == "0")
    {
        return;
    }

    xmlhttp = getXmlHttpObject();
    xmlhttp.onreadystatechange = componentStateChanged;
    xmlhttp.open("POST", url, true);
    xmlhttp.send(null);
}